package com.help10000.rms.common

import android.content.Context
import android.content.Intent
import com.amir.common.utils.AppManager
import com.help10000.rms.ui.activitys.MainActivity
import com.help10000.rms.ui.activitys.logins.LoginActivity
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView

object GlobalNetWorkObserver {
    private var basePopupView: BasePopupView? = null

    fun displayNoNetworkDialog(context: Context) {
        cancelDialog()
        basePopupView =
            XPopup.Builder(context)
                .dismissOnBackPressed(false)
                .dismissOnTouchOutside(false)
                .asConfirm("提示", "当前网络不可用,请检查网络连接", null, "退出", {
                    if (AppManager.getAppManager().currentActivity() is LoginActivity) {
                        AppManager.getAppManager().finishActivity()
                    } else {
                        var intent = Intent(context, MainActivity::class.java)
                        intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                        context.startActivity(intent)
                    }
                }, null, true)
                .show()

    }

    fun cancelDialog() {
        if (basePopupView != null && basePopupView!!.dialog?.isShowing == true) {
            basePopupView?.dialog?.dismiss()
            basePopupView = null
        }
    }
}